package com.xiaoxu.intranetweb.mybatisplusSzappdb.controller.ashGate;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.alibaba.fastjson.JSONObject;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.business.ashGate.AreaBu;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.business.ashGate.AshGateBu;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.dao.UserInfoDao;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.excelEntity.AshGateExcelEn;
import com.xiaoxu.intranetweb.util.PermissionCheck;
import com.xiaoxu.intranetweb.util.SzResult;
import com.xiaoxu.intranetweb.util.WebFunctionUtil;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;

@RestController
@RequestMapping(value = "/ashGate")
public class ashGateController {
    @Autowired
    private AshGateBu ashGateBu;
    @Autowired
    private AreaBu areaBu;
    @Autowired
    private UserInfoDao userInfoDao;
    @Autowired
    private WebFunctionUtil webFunctionUtil;
    /**
     * 料位查询接口
     *
     * @param ashGateName:料位名称 areaId:区域id
     * @return SzResult
     */
    @GetMapping("/getAshGate")
    public SzResult getMateriallevel(
            @RequestParam(value = "ashGateName", required = false) String ashGateName,
            @RequestParam(value = "areaId", required = false) String areaId,
            @RequestParam(value = "page", required = false) Integer  page,
            @RequestParam(value = "pageSize", required = false) Integer pageSize, HttpServletRequest request
    ) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
//        if (!userInfoDao.getUserPermissionWeb(StpUtil.getLoginIdByToken(token).toString()).containsKey("ashGate_getAshGate")) {
//            return SzResult.error("您没有查询料位信息的权限");
//        }
        return ashGateBu.getAshGate(ashGateName, areaId,page,pageSize,plantId);
    }
    //煤场区域下拉框
    @GetMapping("/getCoalGateList")
    public SzResult getCoalGateList() throws Exception {
        return ashGateBu.getCoalGateList();
    }

    /**
     * 区域查询接口
     *
     * @param areaName:区域名称
     * @return SzResult
     */
    @GetMapping("/getArea")
    public SzResult getArea(
            @RequestParam(value = "areaName", required = false) String areaName
            , HttpServletRequest request
    ) throws Exception {
        String token = request.getHeader("Authorization");
//        if (!userInfoDao.getUserPermissionWeb(StpUtil.getLoginIdByToken(token).toString()).containsKey("ashGate_getArea")) {
//            return SzResult.error("您没有查询区域信息的权限");
//        }
        String plantId = request.getHeader("Plant-Id");
        return areaBu.getArea(areaName,plantId);
    }

    //新增区域管理
    @PostMapping("/addAshGate")
    @PermissionCheck("ashGate_add")
    public SzResult addAshGate(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        String ashGateName = jsonObject.getString("ashGateName");
        String ashGateShrtame = jsonObject.getString("ashGateShrtame");
        String ashGateBindChnlNo = jsonObject.getString("ashGateBindChnlNo");
        String ashGateType = jsonObject.getString("ashGateType");
        String areaId = jsonObject.getString("areaId");//区域类型的ID
        String ashGateSpecGravity = jsonObject.getString("ashGateSpecGravity");//比重
        String ashGateCubage = jsonObject.getString("ashGateCubage");//库容积
        String ashGateTotalh = jsonObject.getString("ashGateTotalh");//库总高
        String ashGateConeh = jsonObject.getString("ashGateConeh");//库锥高
        String ashGateAlarmLl = jsonObject.getString("ashGateAlarmLl");//低库位高度
        String ashGateAlarml = jsonObject.getString("ashGateAlarml");//较低库位高度
        String ashGateAlarmh = jsonObject.getString("ashGateAlarmh");//较高库位高度
        String ashGateAlarmHh = jsonObject.getString("ashGateAlarmHh");//报警库位高度
        String ashGateCurHght = jsonObject.getString("ashGateCurHght");//当前库位高度
        String ashGateCurWght = jsonObject.getString("ashGateCurWght");//当前库位对应的重量
        String ashGateCurQlty = jsonObject.getString("ashGateCurQlty");//当前质量
        String ashGateAcqTm = jsonObject.getString("ashGateAcqTm");//采集时间
        String ashGatePdtID = jsonObject.getString("ashGatePdtID");//产品ID
        String ashSubjectId = jsonObject.getString("ashSubjectId");//发货单位id
        Integer ashSn = jsonObject.getInteger("ashSn");//编号
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return ashGateBu.addAshGate(ashGateName, ashGateShrtame, ashGateBindChnlNo, ashGateType, areaId, ashGateSpecGravity, ashGateCubage, ashGateTotalh,
                ashGateConeh, ashGateAlarmLl, ashGateAlarml, ashGateAlarmh, ashGateAlarmHh, ashGateCurHght, ashGateCurWght, ashGateCurQlty,ashGateAcqTm,ashGatePdtID,ashSubjectId,ashSn,token,plantId);

        }

    //删除区域管理
    @GetMapping("/deleteAshGate")
    @PermissionCheck("ashGate_delete")
    public SzResult deleteAshGate(@RequestParam String ashGateId, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return ashGateBu.deleteAshGate(ashGateId, token,plantId);
    }

    //修改区域管理
    @PostMapping("/updateAshGate")
    @PermissionCheck("ashGate_update")
    public SzResult updateAshGate(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        String ashGateId = jsonObject.getString("ashGateId");
        String ashGateName = jsonObject.getString("ashGateName");
        String ashGateShrtame = jsonObject.getString("ashGateShrtame");
        String ashGateBindChnlNo = jsonObject.getString("ashGateBindChnlNo");
        String ashGateType = jsonObject.getString("ashGateType");
        String areaId = jsonObject.getString("areaId");//区域类型的ID
        String ashGateSpecGravity = jsonObject.getString("ashGateSpecGravity");//比重
        String ashGateCubage = jsonObject.getString("ashGateCubage");//库容积
        String ashGateTotalh = jsonObject.getString("ashGateTotalh");//库总高
        String ashGateConeh = jsonObject.getString("ashGateConeh");//库锥高
        String ashGateAlarmLl = jsonObject.getString("ashGateAlarmLl");//低库位高度
        String ashGateAlarml = jsonObject.getString("ashGateAlarml");//较低库位高度
        String ashGateAlarmh = jsonObject.getString("ashGateAlarmh");//较高库位高度
        String ashGateAlarmHh = jsonObject.getString("ashGateAlarmHh");//报警库位高度
        String ashGateCurHght = jsonObject.getString("ashGateCurHght");//当前库位高度
        String ashGateCurWght = jsonObject.getString("ashGateCurWght");//当前库位对应的重量
        String ashGateCurQlty = jsonObject.getString("ashGateCurQlty");//当前质量
        String ashGateAcqTm = jsonObject.getString("ashGateAcqTm");//采集时间
        String ashGatePdtID = jsonObject.getString("ashGatePdtID");//产品ID
        String ashSubjectId = jsonObject.getString("ashSubjectId");//发货单位id
        Integer ashSn = jsonObject.getInteger("ashSn");//编号
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return ashGateBu.updateAshGate(ashGateId, ashGateName, ashGateShrtame, ashGateBindChnlNo, ashGateType, areaId, ashGateSpecGravity, ashGateCubage, ashGateTotalh,
                ashGateConeh, ashGateAlarmLl, ashGateAlarml, ashGateAlarmh, ashGateAlarmHh, ashGateCurHght, ashGateCurWght, ashGateCurQlty,ashGateAcqTm,ashGatePdtID,ashSubjectId,ashSn,token,plantId);
        }

        //是否手动料位
        @GetMapping("/isManual")
        @PermissionCheck("ashGate_isManual")
        public SzResult isManual(@RequestParam String ashGateId,@RequestParam Integer isOrNot, HttpServletRequest request) throws Exception {
            String token = request.getHeader("Authorization");
            String plantId = request.getHeader("Plant-Id");
            return ashGateBu.isManual(ashGateId, isOrNot,token,plantId);
        }

        //设置手动料位高度和操作员
        @GetMapping("/setAshGateCurHghtMan")
        @PermissionCheck("ashGate_setAshGateCurHghtMan")
        public SzResult setAshGateCurHghtMan(@RequestParam String ashGateId,@RequestParam String ashGateCurHght, HttpServletRequest request) throws Exception {
            String token = request.getHeader("Authorization");
            String plantId = request.getHeader("Plant-Id");
            return ashGateBu.setAshGateCurHghtMan(ashGateId, ashGateCurHght,token,plantId);
        }


    /**
     * 导表
     */

    @PostMapping("/exportExcel")
    public void exportExcel(@RequestBody JSONObject jsonParam , HttpServletResponse response, HttpServletRequest request) throws Exception {
//
//        String plantId = null;
//        if (token !=null) {
//            plantId = userInfoDao.getUserFactory(StpUtil.getLoginIdByToken(token).toString()).getId();
//        }
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        //        实体类
        List<Map<String, Object>> fields = jsonParam.getObject("fields",List.class);
//        文件名使用utf-8编码
        String filename = jsonParam.getString("filename");
//        数据
        List<Map<String, Object>> ids = jsonParam.getObject("ids",List.class);
//        是否有表头
        Boolean isHeader = jsonParam.getBoolean("isHeader");
//        mode为"current"时，导出当前页，为"all"时，导出所有页，selected时，导出选中数据
        String mode = jsonParam.getString("mode");
//        是否原始数据
        Boolean original = jsonParam.getBoolean("original");
//        分页
        Map<String, String> pager = jsonParam.getObject("pager",Map.class);
//        表格标题
        String sheetName = jsonParam.getString("sheetName");
        Map form = jsonParam.getObject("form",Map.class);
//        String truckNo, Integer truckType
        String ashGateName = null;
        if ( form.get("ashGateName") != null && !form.get("ashGateName").equals("null")){
            ashGateName = form.get("ashGateName").toString();
        }
        String areaId = null;
        if ( form.get("areaId") != null && !form.get("areaId").equals("null")){
            areaId = form.get("areaId").toString();
        }
        String ashGateBindChnlNo = null;
        if ( form.get("ashGateBindChnlNo") != null && !form.get("ashGateBindChnlNo").equals("null")){
            ashGateBindChnlNo =form.get("ashGateBindChnlNo").toString();
        }
        Future<List<AshGateExcelEn>> listFuture =ashGateBu.exportExcel(fields, filename, ids, isHeader, mode, original, pager, sheetName,areaId, ashGateName, ashGateBindChnlNo, plantId);
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        BorderStyle borderStyle = BorderStyle.THIN;
        short borderColor = IndexedColors.BLACK.getIndex();
        contentWriteCellStyle.setBorderTop(borderStyle);
        contentWriteCellStyle.setBorderBottom(borderStyle);
        contentWriteCellStyle.setBorderLeft(borderStyle);
        contentWriteCellStyle.setBorderRight(borderStyle);
        contentWriteCellStyle.setTopBorderColor(borderColor);
        contentWriteCellStyle.setBottomBorderColor(borderColor);
        contentWriteCellStyle.setLeftBorderColor(borderColor);
        contentWriteCellStyle.setRightBorderColor(borderColor);
        HorizontalCellStyleStrategy horizontalCellStyleStrategy =
                new HorizontalCellStyleStrategy(null, contentWriteCellStyle);
        List<AshGateExcelEn> ashGateExcelEn = listFuture.get();
        response.setContentType("application/vnd.ms-excel; charset=utf-8");
        response.setCharacterEncoding("utf-8");
        if (filename == null || filename.equals("")) {
            filename = "区域信息";
        }
        response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8") + ".xlsx");
        EasyExcel.write(response.getOutputStream(), AshGateExcelEn.class)
                .registerWriteHandler(horizontalCellStyleStrategy)
                .sheet("区域信息")
                .doWrite(ashGateExcelEn);
        response.getOutputStream().flush();
        response.getOutputStream().close();
    }






    /**
     * 查询区域类型
     *
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping("/getAreaType")
    public SzResult getAreaType(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String page = jsonObject.getString("page");
        String pageSize = jsonObject.getString("pageSize");
        String areaName = jsonObject.getString("areaName");
        String areaDesc = jsonObject.getString("areaDesc");
        String bz = jsonObject.getString("bz");
        String plantId = request.getHeader("Plant-Id");
        return ashGateBu.getAreaType(page, pageSize, areaName, areaDesc, bz, token,plantId);
    }

    /**
     * 新增区域类型
     *
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping("/addAreaType")
    public SzResult addAreaType(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String areaName = jsonObject.getString("areaName");
        String areaDesc = jsonObject.getString("areaDesc");
        String bz = jsonObject.getString("bz");
        String plantId = request.getHeader("Plant-Id");
        return ashGateBu.addAreaType(areaName, areaDesc, bz, token,plantId);
    }

    /**
     * 修改区域类型
     *
     * @param jsonObject
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping("/updateAreaType")
    public SzResult updateAreaType(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String areaName = jsonObject.getString("areaName");
        String areaDesc = jsonObject.getString("areaDesc");
        String bz = jsonObject.getString("bz");
        String id = jsonObject.getString("id");
        String plantId = request.getHeader("Plant-Id");
        return ashGateBu.updateAreaType(areaName, areaDesc, bz, id, token,plantId);
    }

    /**
     * 删除区域类型
     *
     * @param id
     * @param request
     * @return
     * @throws Exception
     */
    @GetMapping("/deleteAreaType")
    public SzResult deleteAreaType(@RequestParam String id, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return ashGateBu.deleteAreaType(id, token,plantId);
    }
//修改灰库可容车辆 @TableField("VEHICIE_CAPACITY")
//    private String vehicleCapacity;
    @PostMapping("/updateAshGateVehicleCapacity")
    public SzResult updateAshGateVehicleCapacity(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        String ashGateId = jsonObject.getString("ashGateId");
        Integer VehicleCapacity = jsonObject.getInteger("VehicleCapacity");
        return ashGateBu.updateAshGateVehicleCapacity(ashGateId, VehicleCapacity, token, plantId);
    }

//    是否应急，emergency
//    public SzResult updateAshGateEmergency(String ashGateId, Integer emergency, String token, String plantId) throws Exception {
    @PostMapping("/updateAshGateEmergency")
    public SzResult updateAshGateEmergency(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        String ashGateId = jsonObject.getString("ashGateId");
        Integer emergency = jsonObject.getInteger("emergency");
        return ashGateBu.updateAshGateEmergency(ashGateId, emergency, token, plantId);
    }

    @PostMapping("/updateAshGateAshgateSubject")
    public SzResult updateAshGateAshgateSubject(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        String ashGateId = jsonObject.getString("ashGateId");
        String ashgateSubject = jsonObject.getString("ashgateSubjectId");
        return ashGateBu.updateAshGateAshgateSubject(ashGateId, ashgateSubject, token, plantId);
    }
}
